<template>
    <el-select :value="value" :clearable="clearable" :placeholder="placeholder" style="width:100%" @input="sendChange"
        :disabled="disabled">
        <el-option v-for="item in Options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
    </el-select>
</template>
<script>
    import * as dictService from '@/api/bdms/dict'
    export default {
        props: {
            value: {
                type: String,
                default: ""
            },
            disabled: {
                type: Boolean,
                default: false
            },
            dictType: {
                type: String,
                default: ""
            },
            placeholder: {
                type: String,
                default: "请选择"
            },
            // 默认值
            defaultValue: {
                type: String,
                default: ''
            },
            // 默认下标
            defaultIndex: {
                type: String,
                default: ''
            },
            clearable: {
                type: Boolean,
                default: true
            }
        },
        data() {
            return {
                Options: []
            }
        },
        components: {},
        created() {
            this.getDict()
        },
        methods: {
            // 请求数据
            getDict() {
                dictService.getValues(this.dictType).then(data => {
                    this.Options = data;
                    this.setDefault()
                })
            },
            // 设置默认值
            setDefault() {
                // 没有选中的时候设置默认值
                if (this.$isEmpty(this.value)) {
                    // 如果默认值不为空
                    // console.log("this.defaultValue" + this.defaultValue)
                    if (this.$isNotEmpty(this.defaultValue)) {
                        this.sendChange(this.defaultValue)
                    }
                    // 如果默认下标不为空
                    if (this.$isNotEmpty(this.defaultIndex)) {
                        // console.log("this.defaultIndex" + this.defaultIndex)
                        if (this.Options && this.Options.length > Number(this.defaultValue)) {
                            let obj = this.Options[this.defaultIndex];
                            if (obj) {
                                this.sendChange(obj.value)
                            }
                        }
                    }
                }
            },
            sendChange(currentValue) {
                // console.log("设置默认值：", currentValue)
                this.$emit("input", currentValue);
            }
        }
    }
</script>